package com.yanqu.road.dao.impl.drugstore;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.drugstore.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDrugStoreDataDaoImpl extends TempDataObjectDao<UserDrugStoreData> {


    public static String getUserDrugStoreDataSql = "select * from `t_u_drug_store` where `userId`=?";
    public UserDrugStoreData getUserDrugStoreData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserDrugStoreDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserDrugStoreDataSql = "insert into `t_u_drug_store`(userId,drugStoreLv,power,experience,tongBi,lastRecoverPowerTime,bookKeeperLv,druggistLv,orderNo,rewardList,plantId,maxQueueId,npcQueueList,drugNumMap,unLockDrug,doctorTableList,lastQuitViewTime," +
            "entryViewTime,rankActivityDataMap,buffValue,receiveNum,techList) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    
    @Override
    protected boolean addData(UserDrugStoreData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrugStoreLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExperience()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTongBi()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBookKeeperLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDruggistLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRewardList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlantId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxQueueId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getNpcQueueList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrugNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnLockDrug())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDoctorTableList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastQuitViewTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEntryViewTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBuffValue())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceiveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTechList())));
        if (getDb().executeNoneQuery(insertUserDrugStoreDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserDrugStoreDataSql = "update `t_u_drug_store` set drugStoreLv=?,power=?,experience=?,tongBi=?,lastRecoverPowerTime=?,bookKeeperLv=?,druggistLv=?,orderNo=?,rewardList=?,plantId=?,maxQueueId=?,npcQueueList=?,drugNumMap=?,unLockDrug=?,doctorTableList=?,lastQuitViewTime=?," +
            "entryViewTime=?,rankActivityDataMap=?,buffValue=?,receiveNum=?,techList=? where userId=?";
    
    @Override
    protected boolean updateData(UserDrugStoreData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getDrugStoreLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getExperience()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTongBi()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastRecoverPowerTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getBookKeeperLv()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getDruggistLv()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOrderNo()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRewardList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getPlantId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxQueueId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getNpcQueueList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDrugNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUnLockDrug())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getDoctorTableList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastQuitViewTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEntryViewTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getRankActivityDataMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBuffValue())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReceiveNum()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getTechList())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserDrugStoreDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserDrugStoreData parseData(ResultSet rs) throws SQLException {
        UserDrugStoreData userDrugStoreData = new UserDrugStoreData();
        userDrugStoreData.setUserId(rs.getLong("userId"));
        userDrugStoreData.setDrugStoreLv(rs.getInt("drugStoreLv"));
        userDrugStoreData.setPower(rs.getInt("power"));
        userDrugStoreData.setExperience(rs.getLong("experience"));
        userDrugStoreData.setTongBi(rs.getLong("tongBi"));
        userDrugStoreData.setLastRecoverPowerTime(rs.getLong("lastRecoverPowerTime"));
        userDrugStoreData.setBookKeeperLv(rs.getInt("bookKeeperLv"));
        userDrugStoreData.setDruggistLv(rs.getInt("druggistLv"));
        userDrugStoreData.setOrderNo(rs.getLong("orderNo"));
        userDrugStoreData.setRewardList(JSON.parseObject(rs.getString("rewardList"), new TypeReference<List<DrugOrderReward>>() {}));
        userDrugStoreData.setPlantId(rs.getLong("plantId"));
        userDrugStoreData.setMaxQueueId(rs.getLong("maxQueueId"));
        userDrugStoreData.setNpcQueueList(JSON.parseObject(rs.getString("npcQueueList"), new TypeReference<List<DrugStoreQueue>>() {}));
        userDrugStoreData.setDrugNumMap(JSON.parseObject(rs.getString("drugNumMap"), new TypeReference<Map<Integer, Integer>>() {}));
        userDrugStoreData.setUnLockDrug(JSON.parseObject(rs.getString("unLockDrug"), new TypeReference<List<Integer>>() {}));
        userDrugStoreData.setDoctorTableList(JSON.parseObject(rs.getString("doctorTableList"), new TypeReference<List<DrugStoreDoctorTable>>() {}));
        userDrugStoreData.setLastQuitViewTime(rs.getLong("lastQuitViewTime"));
        userDrugStoreData.setEntryViewTime(rs.getLong("entryViewTime"));
        userDrugStoreData.setRankActivityDataMap(JSON.parseObject(rs.getString("rankActivityDataMap"), new TypeReference<Map<Integer, DrugActivityRankValue>>() {}));
        userDrugStoreData.setBuffValue(JSON.parseObject(rs.getString("buffValue"), new TypeReference<DrugStoreBuffValue>() {}));
        userDrugStoreData.setReceiveNum(rs.getLong("receiveNum"));
        userDrugStoreData.setTechList(JSON.parseObject(rs.getString("techList"), new TypeReference<List<DrugStoreTeach>>() {}));
        return userDrugStoreData;
    }
}
